<template>
	<el-card shadow="never" header="账号信息">
		<el-form ref="form" :model="form" :rules="rules" label-width="120px" style="margin-top:20px;">
			<el-form-item label="账号">
				<el-input v-model="form.username" disabled></el-input>
				<div class="el-form-item-msg">账号信息用于登录，系统不允许修改</div>
			</el-form-item>
			<el-form-item label="姓名" prop="nickname">
				<el-input v-model="form.nickname"></el-input>
			</el-form-item>
			<el-form-item label="性别">
				<el-select v-model="form.sex" placeholder="请选择">
					<el-option label="保密" :value="0"></el-option>
					<el-option label="男" :value="1"></el-option>
					<el-option label="女" :value="2"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="邮箱地址">
				<el-input v-model="form.email"></el-input>
			</el-form-item>
			<el-form-item label="个人签名">
				<el-input v-model="form.signed" type="textarea"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="save">保存</el-button>
			</el-form-item>
		</el-form>
	</el-card>
</template>

<script>
	export default {
		data() {
			return {
				user: {},
				form: {
					username: "administrator@scuiadmin.com",
					nickname: "Sakuya",
					sex: "0",
					email:'',
					signed: "正所谓富贵险中求"
				},
				rules: {
					nickname: [
						{ required: true, message: '请输入姓名'}
					],
					email: [
						{ required: true, message: '请输入邮箱地址'}
					],
				}
			}
		},
		created() {
			this.user = this.$TOOL.data.get("USER_INFO");
			this.init_form()
		},
		methods: {
			init_form() {
				this.form.username = this.user.username
				this.form.nickname = this.user.nickname
				this.form.sex = this.user.sex
				this.form.email = this.user.email
				this.form.signed = this.user.signed
			},
			save() {
				var that = this
				that.$refs.form.validate(async(valid) => {
					if (valid) {
						var res = await that.$API.system.user.modifyUser.put(that.form)
						if(res.code == 200) {
							let user = that.$TOOL.data.get("USER_INFO")
							user.username = that.form.nickname
							user.sex = that.form.sex
							user.email = that.form.email
							user.signed = that.form.signed
							that.$TOOL.data.set("USER_INFO", user)
							that.$alert("个人信息修改成功", "提示", {type: 'success'})
						} else {
							that.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false
					}
				})
			}
		}
	}
</script>

<style>
</style>
